package playing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Login extends JFrame {
    private int loggedInPlantId = 0;

    public Login() {

        // 设置窗口标题和大小
        setTitle("登录界面");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(4, 1));

        JLabel welcomeLabel = new JLabel("欢迎来到pp农场", SwingConstants.CENTER);
        mainPanel.add(welcomeLabel);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());

        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loggedInPlantId = register();
                if (loggedInPlantId != 0) {
                    JOptionPane.showMessageDialog(null, "你的用户ID是: " + loggedInPlantId + "\n已自动登入");
                    dispose(); // 关闭当前窗口
                    new menu(loggedInPlantId);
                }
            }
        });
        buttonPanel.add(registerButton);

        JButton loginButton = new JButton("登入");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loggedInPlantId = login();
                if (loggedInPlantId != 0) {
                    JOptionPane.showMessageDialog(null, "你的用户ID是: " + loggedInPlantId + "\n你已成功进入pp农场");
                    dispose(); // 关闭当前窗口
                    new menu(loggedInPlantId);
                }
            }
        });
        buttonPanel.add(loginButton);

        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        buttonPanel.add(exitButton);

        mainPanel.add(buttonPanel);
        add(mainPanel);
        setVisible(true);
    }

    private int register() {
        String u = JOptionPane.showInputDialog(this, "请输入用户名");
        if (u == null || u.isEmpty()) return 0;

        String p = JOptionPane.showInputDialog(this, "请输入密码");
        if (p == null || p.isEmpty()) return 0;

        PlantLogin login = new PlantLogin();
        // 注册新用户
        int plantId = login.register(u, p);
        return plantId;
    }

    private int login() {
        String u = JOptionPane.showInputDialog(this, "请输入用户名");
        if (u == null || u.isEmpty()) return 0;

        String p = JOptionPane.showInputDialog(this, "请输入密码");
        if (p == null || p.isEmpty()) return 0;

        PlantLogin login = new PlantLogin();
        // 用户登录
        int loggedInPlantId = login.login(u, p);
        if (loggedInPlantId != -1) {
            return loggedInPlantId;
        } else {
            JOptionPane.showMessageDialog(this, "用户名或密码错误,登入失败", "登录失败", JOptionPane.ERROR_MESSAGE);
            return 0;
        }
    }

}
